<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>商品详情</title>
    <script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.5.1.min.js"></script>
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            margin: 0;
            padding: 20px;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        .container {
            background-color: #ffffff;
            padding: 30px;
            border-radius: 16px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
            width: 90%;
            max-width: 800px;
            margin: 20px auto;
            display: grid;
            grid-template-columns: 1fr 1fr;
            grid-gap: 30px;
        }
        
        .header {
            grid-column: 1 / -1;
            text-align: center;
            margin-bottom: 20px;
            position: relative;
        }
        
        h1 {
            color: #2c3e50;
            font-size: 2.5rem;
            margin-bottom: 5px;
            position: relative;
            display: inline-block;
            padding-bottom: 10px;
        }
        
        h1::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 50%;
            transform: translateX(-50%);
            width: 80px;
            height: 4px;
            background: linear-gradient(to right, #3498db, #2ecc71);
            border-radius: 2px;
        }
        
        .product-image-container {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 20px;
            background: #f8f9fa;
            border-radius: 12px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);
        }
        
        .product-image {
            width: 100%;
            max-height: 300px;
            object-fit: contain;
            border-radius: 8px;
            transition: transform 0.3s ease;
            cursor: pointer;
            margin-bottom: 20px;
            border: 1px solid #eee;
            padding: 10px;
            background: white;
        }
        
        .product-image:hover {
            transform: scale(1.03);
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
        }
        
        .image-source {
            font-size: 0.9rem;
            color: #7f8c8d;
            text-align: center;
            margin-top: 10px;
            word-break: break-all;
        }
        
        .product-details {
            display: flex;
            flex-direction: column;
            gap: 20px;
        }
        
        .detail-group {
            background: #f8f9fa;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.03);
            transition: transform 0.2s ease;
        }
        
        .detail-group:hover {
            transform: translateY(-3px);
            box-shadow: 0 6px 12px rgba(0, 0, 0, 0.08);
        }
        
        .detail-label {
            font-size: 0.9rem;
            font-weight: 600;
            color: #3498db;
            margin-bottom: 8px;
            display: flex;
            align-items: center;
        }
        
        .detail-label i {
            margin-right: 8px;
            font-size: 1.1rem;
        }
        
        .detail-value {
            font-size: 1.2rem;
            color: #2c3e50;
            font-weight: 500;
            padding: 8px 0;
            border-bottom: 1px dashed #e0e0e0;
        }
        
        .price-value {
            color: #e74c3c;
            font-size: 1.8rem;
            font-weight: 700;
        }
        
        .stock-value {
            color: #27ae60;
            font-weight: 700;
        }
        
        .actions {
            grid-column: 1 / -1;
            display: flex;
            justify-content: center;
            gap: 20px;
            margin-top: 20px;
        }
        
        .btn {
            padding: 14px 30px;
            border: none;
            border-radius: 50px;
            font-size: 1rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            justify-content: center;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }
        
        .btn i {
            margin-right: 8px;
        }
        
        .btn-back {
            background: linear-gradient(to right, #3498db, #2980b9);
            color: white;
        }
        
        .btn-back:hover {
            background: linear-gradient(to right, #2980b9, #2573a7);
            transform: translateY(-2px);
            box-shadow: 0 6px 12px rgba(52, 152, 219, 0.3);
        }
        
        .btn-edit {
            background: linear-gradient(to right, #2ecc71, #27ae60);
            color: white;
        }
        
        .btn-edit:hover {
            background: linear-gradient(to right, #27ae60, #219d54);
            transform: translateY(-2px);
            box-shadow: 0 6px 12px rgba(46, 204, 113, 0.3);
        }
        
        .product-id {
            position: absolute;
            top: 20px;
            right: 20px;
            background: #e74c3c;
            color: white;
            padding: 5px 12px;
            border-radius: 20px;
            font-size: 0.85rem;
            font-weight: 600;
        }
        
        .sales-badge {
            display: inline-block;
            background: #f39c12;
            color: white;
            padding: 4px 12px;
            border-radius: 12px;
            font-size: 0.85rem;
            margin-top: 10px;
        }
        
        @media (max-width: 768px) {
            .container {
                grid-template-columns: 1fr;
            }
            
            .header h1 {
                font-size: 2rem;
            }
            
            .actions {
                flex-direction: column;
            }
        }
    </style>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>
<body>
<div class="container">

    <div class="header">
        <h1>商品详情</h1>
    </div>

  <div class="detail-group" id="promoStartDateContainer">
            <div class="detail-label">
                <i class="fas fa-tags" id="promoStatus"></i>
            </div>
            <div class="detail-value price-value" id="promoStartDate" ></div>
        </div>


       <div class="header">
    </div>
    <div class="product-image-container">
        <img class="product-image"
             id="imgUrl">
    </div>
    <div class="product-details">
        <div class="detail-group">
            <div class="detail-label">
                <i class="fas fa-tag"></i> 商品名称
            </div>
            <div class="detail-value" id="title"></div>
        </div>
        
        <div class="detail-group">
            <div class="detail-label">
                <i class="fas fa-align-left"></i> 商品描述
            </div>
            <div class="detail-value" id="description"></div>
        </div>
        
        <div class="detail-group" id="normalPriceContainer">
            <div class="detail-label">
                <i class="fas fa-tags"></i> 商品价格
            </div>
            <div class="detail-value price-value" id="price"></div>
        </div>

        <div class="detail-group" id="promoPriceContainer">
            <div class="detail-label">
                <i class="fas fa-tags"></i> 秒杀价格
            </div>
            <div class="detail-value price-value" id="promoPrice" ></div>
        </div>
        
        <div class="detail-group">
            <div class="detail-label">
                <i class="fas fa-box"></i> 商品库存
            </div>
            <div class="detail-value stock-value" id="stock"></div>
        </div>
        
        <div class="detail-group">
            <div class="detail-label">
                <i class="fas fa-chart-line"></i> 销售数据
            </div>
            <div class="detail-value">
                已售出：<span class="sales-badge" id="sales"></span>
            </div>
        </div>
    </div>

    <div class="actions">

        <button class="btn btn-back" onclick="location.href='listitem.html'">
            <i class="fas fa-arrow-left"></i> 返回商品列表
        </button>

        <button class="btn btn-back" id="createorder">
            <i class="fas fa-arrow"></i> 下单
        </button>

    </div>
</div>

<script>
  function getParam(paramName) { 
    paramValue = "", isFound = !1; 
    if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) { 
        arrSource = unescape(this.location.search).substring(1, this.location.search.length).split("&"), i = 0; 
        while (i < arrSource.length && !isFound) arrSource[i].indexOf("=") > 0 && arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase() && (paramValue = arrSource[i].split("=")[1], isFound = !0), i++ 
    } 
    return paramValue == "" && (paramValue = null), paramValue 
} 
var g_itemVO={}
    jQuery(document).ready(function(){
        $("#createorder").on("click",function(){
            $.ajax({
          type:"POST",
          contentType:"application/x-www-form-urlencoded",
          url:"http://localhost:8090/order/createorder",
          data:{
            "itemId":g_itemVO.id,
            "amount":1,
            "promoId":g_itemVO.promoId
          },
          xhrFields: {
        withCredentials: true
    },
          success:function(data){
          if(data.status=="success"){
           alert("下单成功");
           window.location.reload();
          }else{
          alert("下单失败，原因为"+data.data.errMsg)
           if(data.data.errCode==20003){
            window.location.href="/miaoshaoqiangduan/login.html";
          }
          }
          },
          error:function(data){
          alert("下单失败，原因为"+data.responseText);
          }
          });
          return false;
        });
        //获取商品详情
          $.ajax({
          type:"GET",
          contentType:"application/x-www-form-urlencoded",
          url:"http://localhost:8090/item/get",
          data:{
            "id":getParam("id"),
          },
          xhrFields: {
        withCredentials: true
    },
          success:function(data){
          if(data.status=="success"){
            g_itemVO=data.data;
            reloadDom();
            setInterval(reloadDom,1000);
          }else{
          alert("获取商品信息失败，原因为"+data.data.errMsg)
          }
          },
          error:function(data){
          alert("获取商品信息失败，原因为"+data.responseText);
          }
          });
          return false;
        });
    function reloadDom(){
        $("#title").text(g_itemVO.title);
        $("#description").text(g_itemVO.description);
        $("#stock").text(g_itemVO.stock);
        $("#price").text(g_itemVO.price);
        $("#imgUrl").attr("src",g_itemVO.imgUrl);
        $("#sales").text(g_itemVO.sales);
        if(g_itemVO.promoStatus==1){
            //秒杀活动还未开始
            var startTime=g_itemVO.promoStartDate.replace(new RegExp("-","gm"),"/");
            startTime=(new Date(startTime)).getTime();
            var nowTime=Date.parse(new Date());
            var delta=(startTime-nowTime)/1000;
            if(delta<=0){
                //活动开始
                g_itemVO.promoStatus=2;
                reloadDom();
            }
            $("#promoStartDate").text("秒杀活动将于："+g_itemVO.promoStartDate+"开始售卖 倒计时:"+delta+"秒");
            $("#promoPrice").text(g_itemVO.promoPrice);
            $("#createorder").attr("disabled",true)
        }else if(g_itemVO.promoStatus==2){
            //秒杀进行中
            $("#promoStartDate").text("秒杀活动正在进行中");
            $("#promoPrice").text(g_itemVO.promoPrice);
            $("#createorder").attr("disabled",false);
            $("#normalPriceContainer").hide();
        }
    }
</script>
</body>
</html>